package com.oceansoft.mobile.econsole.modules.weixin.controller;

import com.google.common.base.Strings;
import com.oceansoft.mobile.econsole.common.constant.Constant;
import com.oceansoft.mobile.econsole.modules.base.controller.BaseController;
import com.oceansoft.mobile.econsole.modules.base.entity.Result;
import com.oceansoft.mobile.econsole.modules.member.entity.WtMember;
import com.oceansoft.mobile.econsole.modules.member.service.IMemberService;
import com.oceansoft.mobile.econsole.modules.member.service.IWtMemberService;
import com.oceansoft.mobile.econsole.modules.weixin.config.WxFactory;
import com.oceansoft.mobile.econsole.weixin.api.OauthAPI;
import com.oceansoft.mobile.econsole.weixin.api.enums.OauthScope;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import java.util.Objects;

import static org.springframework.web.bind.annotation.RequestMethod.GET;

/**
 * Created by Administrator on 2016/3/2.
 */
@Controller
@RequestMapping("weixin/percenter")
public class PerCenterController extends BaseController {

    @Resource
    private IMemberService memberService;
    @Resource
    private IWtMemberService wtMemberservice;

    @Resource
    private IWtMemberService wtMemberMapper;

    /**
     * 进入个人中心oauth认证
     */
    @RequestMapping(value = "oauth", method = GET)
    public String gotoPerCenterOAuth(HttpServletRequest request) {
        OauthAPI oauthAPI = WxFactory.buildOauthAPI();
        return "redirect:" + oauthAPI.getOauthPageUrl(WxFactory.PERCENTER_OAUTH_URL, OauthScope.SNSAPI_BASE, "123");
    }

    /**
     * 个人中心首页
     */
    @RequestMapping(value = "index")
    public String gotoWeixinIndex(HttpServletRequest request, Model model) throws Exception {
        WtMember member = getWxMemberSession();
        // 未登陆
        if (Objects.isNull(member)) {
            String pt = request.getParameter("pt");
            if (!Objects.equals("A", pt) && !Objects.equals("I", pt)) {
                String openId = super.getOpenId(request);
                openId = Strings.isNullOrEmpty(openId) ? "123456000" : openId; //兼容测试数据
                model.addAttribute("openId", openId);
            }
            model.addAttribute("gowhere", "/weixin/percenter/index");//登陆以后进入个人中心
            return "weixin/WX_Member/wx_login";
        }

        model.addAttribute("userGuid", member.getGuid());
        // 获取实名认证信息
        model.addAttribute("rzResult", member.getIdentifystatus());//注册的时候就获取认证状态
        return "weixin/WX_Member/personal_center";
    }

    /**
     * 用户微信注销登录
     *
     * @param request
     * @return
     */
    @ResponseBody
    @RequestMapping("userOut")
    public Result userOut(HttpServletRequest request) {
        //登录时memberServiceImpl中的wxAuthenticationProcess方法 更改数据openId，退出登录也更改openId
//        wtMemberMapper.updateOpenidByUserName(getWxMemberSession().getUserName(), "20");
//        request.getSession().getAttribute(Constant.SESSION_MEMBER);
        request.getSession().removeAttribute(Constant.SESSION_USER);
        request.getSession().removeAttribute(Constant.SESSION_MEMBER);
//        request.getSession().removeAttribute("openid");
        return new Result(Result.SUCC, "登出成功!");
    }

    @ResponseBody
    @RequestMapping("loginout")
    public Result loginout() {
        WtMember member = getWxMemberSession();
        Result result = this.loginout(member.getGuid(), member.getWeixinid());
        if (result.isSucc()) {
            HttpSession httpSession = getHttpSession();
            httpSession.removeAttribute(Constant.SESSION_USER);
            httpSession.removeAttribute(Constant.SESSION_MEMBER);
        }

        return result;
    }
}
